package com.client.service;

import java.util.List;

import org.apache.log4j.Logger;

import com.client.model.ChannelSdkiPayData;
import com.client.model.CpSdkIPayData;
import com.client.model.IPayData;
import com.client.model.SdkIPayData;
import com.client.model.SdkIPayOrder;
import com.client.model.SdkPayOrderCode;
import com.manage.config.Config;
import com.service.AbstractService;
import com.util.ConfigUtil;
import com.utils.dbcp.ConnectionManager;
import com.utils.dbcp.ModelGenerator;

public class SdkIPayOrderServiceImpl extends AbstractService implements SdkIPayOrderService {
	
	private Logger logger;
	
	public SdkIPayOrderServiceImpl()
	{
		logger = Logger.getLogger(this.getClass());
		this.dbcp = ConfigUtil.get(Config.SECTION, Config.KEY_DBCP_NAME);
	}
	@Override
	public boolean savePayOrder(SdkIPayOrder po) {
		po.setId(getTableId("sdkipayorder"));
		String sql = "insert into sdkipayorder(id,orderid,suborderid,userid" +
				",mobileno,carrier,province,region,feevalue," +
				"channelid,projectid,report,ordertime" +
				",reporttime" +
				",cpid,appid,goodscode,goodsname,chid,requestfee,successfee,imsi,sdkid,reportsdkid,isfilter) " +
				" values ("+po.getId()+",'"+po.getOrderId()+"','"+
				po.getSuborderId()+"','"+po.getUserId()+"','"+po.getMobileNo()+
				"','"+po.getCarrier()+"','"+po.getProvince()+
				"','"+po.getRegion()+"','"+po.getFeeValue()+"','"+po.getChannelId()+
				"','"+po.getProjectId()+"','"+po.getReport()+"','"+com.util.Util.dateToString(po.getOrderTime(), "yyyy-MM-dd HH:mm:ss") +
				"','"+com.util.Util.dateToString(po.getReportTime(), "yyyy-MM-dd HH:mm:ss")+"'" +
				",'"+po.getCpid()+"','"+po.getAppid()+"','"+po.getGoodscode()+"'" +
				",'"+po.getGoodsname()+"','"+po.getChid()+"','"+po.getRequestfee()+"','"+po.getSuccessfee()+"'" +
				",'"+po.getImsi()+"','"+po.getSdkid()+"','"+po.getReportsdkid()+"','"+po.getIsfilter()+"')";
		return ConnectionManager.getInstance().execute(dbcp, sql);
	}

	@Override
	public boolean updatePayOrder(SdkIPayOrder po) {
		String sql = "update sdkipayorder set report='"+po.getReport()+"',reporttime='"
		+com.util.Util.dateToString(po.getReportTime(), "yyyy-MM-dd HH:mm:ss")+"'" +
				",successfee='"+po.getSuccessfee()+"',reportsdkid='"+po.getReportsdkid()+"' where orderid='"+po.getOrderId()+"'";
		return ConnectionManager.getInstance().execute(dbcp, sql);
	}
	@Override
	public boolean savePayData(SdkIPayData iPayData) {
		iPayData.setId(getTableId("sdkipaydata"));
		String sql ="insert into sdkipaydata(" +
				"id,day,hour,minute,channel" +
				",project,carrier,province," +
				"region,count,resultcount,succ,feevalue,datakey,issuedcount" +
				",cpid,appid,goodscode,goodsname,chid,requestfee,successfee,sdkid,reportsdkid) " +
				" values ('"+iPayData.getId()+
				"','"+iPayData.getDay()+"','"+iPayData.getHour()+
				"','"+iPayData.getMinute()+"','"+iPayData.getChannel()+
				"','"+iPayData.getProject()+"','"+iPayData.getCarrier()+
				"','"+iPayData.getProvince()+"','"+iPayData.getRegion()+
				"','"+iPayData.getCount()+"','"+iPayData.getResultcount()+"','"+iPayData.getSucc()+
				"','"+String.valueOf((int)iPayData.getFeevalue())+"','"+iPayData.getDataKey()+"','"+iPayData.getIssuedcount()+"'" +
				",'"+iPayData.getCpid()+"','"+iPayData.getAppid()+"','"+iPayData.getGoodscode()+"'" +
				",'"+iPayData.getGoodsname()+"','"+iPayData.getChid()+"'" +
				",'"+String.valueOf((int)iPayData.getRequestfee())+
				"','"+String.valueOf((int)iPayData.getSuccessfee())+
				"','"+iPayData.getSdkid()+
				"','"+iPayData.getReportsdkid()+"')";
		return ConnectionManager.getInstance().execute(dbcp, sql);
	}

	@Override
	public boolean updatePayData(SdkIPayData iPayData) {
		String sql = "update sdkipaydata set count='"+iPayData.getCount()+"',issuedcount='"+iPayData.getIssuedcount()+"'," +
				  	"succ='"+iPayData.getSucc()+"',resultcount='"+iPayData.getResultcount()+
				  	"',feevalue='"+String.valueOf((int)iPayData.getFeevalue())+"'" +
				  	",requestfee='"+String.valueOf((int)iPayData.getRequestfee())+
				  	"',successfee='"+String.valueOf((int)iPayData.getSuccessfee())+"',reportsdkid='"+iPayData.getReportsdkid()+"' where datakey='"+iPayData.getDataKey()+"'";
		return ConnectionManager.getInstance().execute(dbcp, sql);
	}
	@Override
	public SdkIPayOrder getPayOrderByOrderId(String orderId) {
		String sql = "select id,orderid,suborderid,userid" +
				",mobileno,carrier,ordertype,codetype," +
				"spid,spcodeid,province,region,feevalue," +
				"channelid,projectid,report,ordertime" +
				",reporttime from sdkipayorder where orderid='"+orderId+"'";
		ModelGenerator<SdkIPayOrder> dbcpmg = new ModelGenerator<SdkIPayOrder>(SdkIPayOrder.class);
		return dbcpmg.generate(dbcp, sql);
	}
	@Override
	public boolean saveSdkpayordercode(List<SdkPayOrderCode> list) {
		String sql[] = new String[list.size()];
		if(list!=null)
		{
			for(int i = 0;i<list.size();i++)
			{
				SdkPayOrderCode code = list.get(i);
				code.setId(getTableId("sdkpayordercode"));
				sql[i] ="insert into sdkpayordercode(" +
						"id,orderid,ordertype,codetype,spid,spcodeid,item) " +
						" values ('"+code.getId()+
						"','"+code.getOrderid()+"','"+code.getOrderType()+
						"','"+code.getCodeType()+"','"+code.getSpId()+
						"','"+code.getSpCodeId()+"','"+code.getItem()+
						"')";
			}
			ConnectionManager.getInstance().executeBatch(dbcp, sql);
		}
		return true;
	}
	@Override
	public boolean saveCpPayData(CpSdkIPayData iPayData) {
		iPayData.setId(getTableId("cpsdkipaydata"));
		String sql ="insert into cpsdkipaydata(" +
				"id,day,hour,minute,channel" +
				",project,carrier,province," +
				"region,count,resultcount,succ,feevalue,datakey,issuedcount" +
				",cpid,appid,goodscode,goodsname,chid,requestfee,successfee,sdkid,reportsdkid) " +
				" values ('"+iPayData.getId()+
				"','"+iPayData.getDay()+"','"+iPayData.getHour()+
				"','"+iPayData.getMinute()+"','"+iPayData.getChannel()+
				"','"+iPayData.getProject()+"','"+iPayData.getCarrier()+
				"','"+iPayData.getProvince()+"','"+iPayData.getRegion()+
				"','"+iPayData.getCount()+"','"+iPayData.getResultcount()+"','"+iPayData.getSucc()+
				"','"+String.valueOf((int)iPayData.getFeevalue())+"','"+iPayData.getDataKey()+"','"+iPayData.getIssuedcount()+"'" +
				",'"+iPayData.getCpid()+"','"+iPayData.getAppid()+"','"+iPayData.getGoodscode()+"'" +
				",'"+iPayData.getGoodsname()+"','"+iPayData.getChid()+"'" +
				",'"+String.valueOf((int)iPayData.getRequestfee())+
				"','"+String.valueOf((int)iPayData.getSuccessfee())+
				"','"+iPayData.getSdkid()+
				"','"+iPayData.getReportsdkid()+"')";
		return ConnectionManager.getInstance().execute(dbcp, sql);
	}
	@Override
	public boolean updateCpPayData(CpSdkIPayData iPayData) {
		String sql = "update cpsdkipaydata set count='"+iPayData.getCount()+"',issuedcount='"+iPayData.getIssuedcount()+"'," +
	  	"succ='"+iPayData.getSucc()+"',resultcount='"+iPayData.getResultcount()+
	  	"',feevalue='"+String.valueOf((int)iPayData.getFeevalue())+"'" +
	  	",requestfee='"+String.valueOf((int)iPayData.getRequestfee())+
	  	"',successfee='"+String.valueOf((int)iPayData.getSuccessfee())+"',reportsdkid='"+iPayData.getReportsdkid()+"' where datakey='"+iPayData.getDataKey()+"'";
		return ConnectionManager.getInstance().execute(dbcp, sql);
	}
	@Override
	public boolean saveChannelPayData(ChannelSdkiPayData iPayData) {
		iPayData.setId(getTableId("channelsdkipaydata"));
		String sql ="insert into channelsdkipaydata(" +
				"id,day,channel" +
				",project,appid,successfee,datakey) " +
				" values ('"+iPayData.getId()+
				"','"+iPayData.getDay()+"','"+iPayData.getChannel()+
				"','"+iPayData.getProject()+"','"+iPayData.getAppid()+"','"+String.valueOf((int)iPayData.getSuccessfee())+
				"','"+iPayData.getDatakey()+"')";
		return ConnectionManager.getInstance().execute(dbcp, sql);
	}
	@Override
	public boolean updateChannelPayData(ChannelSdkiPayData iPayData) {
		String sql = "update channelsdkipaydata set successfee='"+String.valueOf((int)iPayData.getSuccessfee())
		+"' where datakey='"+iPayData.getDatakey()+"'";
		return ConnectionManager.getInstance().execute(dbcp, sql);
	}
	

}
